﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Civic.Platform.Teach.Dao.Base
{
    public class SqlSuagrHelper<T> where T : class, new()
    {
        #region 更新

        /// <summary>
        /// labmda表达式传递条件进行修改
        /// </summary>
        /// <param name="expression1"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static bool UpdToLambda(Expression<Func<T, bool>> expression, Expression<Func<T, bool>> expression1, SqlSugarClient client)
        {
            bool isSuccess;
            using (client)
            {
                //client.BeginTran();
                try
                {
                    isSuccess = client.Updateable<T>().Where(expression).SetColumns(expression1).ExecuteCommand() > 0;
                }
                catch (Exception ex)
                {

                    throw;
                }
                
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据字典所传字段进行更新
        /// </summary>
        /// <param name="dic">字典键值对 key:数据表字段  value:要修改和修改标识值</param>
        /// <param name="tableName">表名字</param>
        /// <param name="key">主键或其他判断条件字段</param>
        /// <returns></returns>
        public static bool UpdToDictionary(Dictionary<string, object> dic, string tableName, string key)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                client.BeginTran();
                isSuccess = client.Updateable<T>(dic).AS(tableName).WhereColumns(key).ExecuteCommandHasChange();
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据实体或者集合更新 --实体或集合中为null的字段不会更新
        /// </summary>
        /// <param name="updList">集合</param>
        /// <param name="key">主键或其他修改依据字段</param>
        /// <param name="noUpd">另外指定不更新的字段 如:主键</param>
        /// <returns></returns>
        public static bool UpdForListClass(List<T> updList, string key, string[] noUpd, SqlSugarClient client)
        {
            bool isSuccess = false;
            using (client)
            {
                client.BeginTran();
                foreach (var item in updList)
                {
                    var upd = client.Updateable<T>(item);
                    upd.IgnoreColumns(ignoreAllNullColumns: true);
                    upd.IgnoreColumns(noUpd);
                    upd.WhereColumns(key);
                    isSuccess = upd.ExecuteCommandHasChange();
                }
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据实体或者集合更新 --实体或集合中为null的字段不会更新
        /// </summary>
        /// <param name="updList">集合</param>
        /// <param name="key">主键或其他修改依据字段</param>
        /// <param name="noUpd">另外指定不更新的字段 如:主键</param>
        /// <returns></returns>
        public static bool UpdForListClass(List<T> updList, string[] key, string[] noUpd, SqlSugarClient client)
        {
            bool isSuccess = false;
            using (client)
            {
                client.BeginTran();
                foreach (var item in updList)
                {
                    var upd = client.Updateable<T>(item);
                    upd.IgnoreColumns(ignoreAllNullColumns: true);
                    upd.IgnoreColumns(noUpd);
                    upd.WhereColumns(key);
                    isSuccess = upd.ExecuteCommandHasChange();
                }
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据实体或者集合更新 --实体或集合中为null的字段不会更新
        /// </summary>
        /// <param name="updList">集合</param>
        /// <param name="key">主键或其他修改依据字段</param>
        /// <param name="noUpd">另外指定不更新的字段 如:主键</param>
        /// <returns></returns>
        public static bool UpdToClass(T updList, string key, string[] noUpd)
        {
            bool isSuccess = false;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                client.BeginTran();
                var upd = client.Updateable<T>(updList);
                upd.IgnoreColumns(ignoreAllNullColumns: true);
                upd.IgnoreColumns(noUpd);
                upd.WhereColumns(key);
                isSuccess = upd.ExecuteCommandHasChange();
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据DataTable 进行更新
        /// </summary>
        /// <param name="dt">DataTable数据</param>
        /// <param name="tableName">数据表名</param>
        /// <param name="key">主键或者其他修改依据字段</param>
        /// <returns></returns>
        public static bool UpdToDataTable(DataTable dt, string tableName, string key)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                client.BeginTran();
                isSuccess = client.Updateable<T>(dt).AS(tableName).WhereColumns(key).ExecuteCommandHasChange();
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 修改--防止并发避免别人已经修改过而自己打开的页面没有刷新造成把别人的数据修改掉
        /// </summary>
        /// <param name="dataList">要修改的集合</param>
        /// <param name="expression">和现有数据库表中字段参数进行比对 如：y => y.Id == it.Item.Id && y.time != it.Item.time</param>
        /// <param name="expression1">如果没有人修改，需要修改的时间字段 如：it=>it.time==DateTime.Now</param>
        /// <returns></returns>
        public static bool UpdToOptimisticLock(List<T> dataList, Func<T, bool> expression, Expression<Func<T, bool>> expression1)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                var x = client.Storageable(dataList)  //list中time和现有数据不相等->错误
                              .SplitError(it => it.Any(expression), "其他人已经编辑过该数据")
                              .SplitUpdate(it => it.Any())  //list 非错误数据 （数据库有这条记录根据主键） ->更新
                              .ToStorage();
                if (x.ErrorList.Any())//时间与数据库时间不相等提示错误
                {
                    isSuccess = false;//throw new Exception(x.ErrorList.First().StorageMessage);

                }
                else
                {
                    //如果时间相等说明这条记录没人占用，更新数据和time
                    isSuccess = x.AsUpdateable.IgnoreColumns(ignoreAllNullColumns: true)
                        .ReSetValue(expression1).ExecuteCommand() > 0;
                }

            }
            return isSuccess;
        }

        /// <summary>
        /// 修改--防止并发与空字段指定不更新
        /// </summary>
        /// <param name="dataList">要修改的集合</param>
        /// <param name="expression">和现有数据库表中字段参数进行比对 如：y => y.Id == it.Item.Id && y.time != it.Item.time</param>
        /// <param name="expression1">如果没有人修改，需要修改的时间字段 如：it=>it.time==DateTime.Now</param>
        /// <param name="key">主键或其他修改依据字段</param>
        /// <param name="noUpd">另外指定不更新的字段 如:主键</param>
        /// <returns></returns>
        public static bool UpdToOptimisticLock(List<T> dataList, Func<StorageableInfo<T>, bool> conditions, Expression<Func<T, bool>> expression1, string key, string[] noUpd)
        {
            bool isSuccess = false;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {

                var x = client.Storageable(dataList)  //list中time和现有数据不相等->错误
                              .SplitError(conditions)
                              .SplitUpdate(it => it.Any())  //list 非错误数据 （数据库有这条记录根据主键） ->更新
                              .ToStorage();
                if (x.ErrorList.Any())//时间与数据库时间不相等提示错误
                {
                    isSuccess = false;//throw new Exception(x.ErrorList.First().StorageMessage);

                }
                else
                {
                    foreach (var item in dataList)
                    {
                        var upd = client.Updateable<T>(item);
                        upd.IgnoreColumns(ignoreAllNullColumns: true);
                        upd.IgnoreColumns(noUpd);
                        upd.WhereColumns(key);
                        upd.ReSetValue(expression1);
                        isSuccess = upd.ExecuteCommandHasChange();
                    }
                    if (isSuccess)
                    {
                        client.CommitTran();
                    }
                    else
                    {
                        client.RollbackTran();
                    }
                }

            }
            return isSuccess;
        }

        /// <summary>
        /// 修改--防止并发与空字段指定不更新
        /// </summary>
        /// <param name="dataList">要修改的集合</param>
        /// <param name="expression">和现有数据库表中字段参数进行比对 如：y => y.Id == it.Item.Id && y.time != it.Item.time</param>
        /// <param name="expression1">如果没有人修改，需要修改的时间字段 如：it=>it.time==DateTime.Now</param>
        /// <param name="key">多条件修改如：y => y.Id == it.Item.Id && y.time != it.Item.time</param>
        /// <param name="noUpd">另外指定不更新的字段 如:主键</param>
        /// <returns></returns>
        public static bool UpdToOptimisticLock(List<T> dataList, Func<StorageableInfo<T>, bool> conditions, Expression<Func<T, bool>> expression1, Expression<Func<T, object>> key, string[] noUpd)
        {
            bool isSuccess = false;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {

                var x = client.Storageable(dataList)  //list中time和现有数据不相等->错误
                              .SplitError(conditions)
                              .SplitUpdate(it => it.Any())  //list 非错误数据 （数据库有这条记录根据主键） ->更新
                              .ToStorage();
                if (x.ErrorList.Any())//时间与数据库时间不相等提示错误
                {
                    isSuccess = false;//throw new Exception(x.ErrorList.First().StorageMessage);

                }
                else
                {
                    foreach (var item in dataList)
                    {
                        var upd = client.Updateable<T>(item);
                        upd.IgnoreColumns(ignoreAllNullColumns: true);
                        upd.IgnoreColumns(noUpd);
                        upd.WhereColumns(key);
                        upd.ReSetValue(expression1);
                        isSuccess = upd.ExecuteCommandHasChange();
                    }
                    if (isSuccess)
                    {
                        client.CommitTran();
                    }
                    else
                    {
                        client.RollbackTran();
                    }
                }

            }
            return isSuccess;
        }

        #endregion

        #region 更新或插入 类似于MerginInto但是不会死锁

        /// <summary>
        /// 插入或者修改
        /// </summary>
        /// <param name="obj">传入的数据，集合</param>
        /// <param name="key">判断是否已存在的唯一字段  new {it.id,it.name}</param>
        /// <returns></returns>
        public static bool InsertOrUpd(List<T> dataList, Expression<Func<T, object>> expression)
        {
            bool isSuccess = false;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                client.BeginTran();
                var x = client.Storageable<T>(dataList).WhereColumns(expression)//当然支持多个 new {it.id,it.name}
                              .ToStorage();//将数据进行分组 

                //isSuccess = x.AsInsertable.ExecuteCommand() > 0; //执行插入
                //isSuccess = x.AsUpdateable.ExecuteCommand() > 0; //执行更新　

                if (x.AsInsertable.ExecuteCommand() > 0 || x.AsUpdateable.ExecuteCommand() > 0)
                {
                    isSuccess = true;
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        #endregion

        #region 新增

        /// <summary>
        /// 根据所传类进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertClass(T dataList)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据所传集合进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertAnyClass(List<T> dataList, SqlSugarClient client)
        {
            bool isSuccess;
            using (client)
            {
                isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 新增操作--部分列不进行插入
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <param name="columns">不需要插入的列的数组</param>
        /// <returns></returns>
        public static bool InsertAnyClassNotColumns(List<T> dataList, string[] columns)
        {
            bool iscuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                iscuccess = client.Insertable<T>(dataList).IgnoreColumns(columns).ExecuteCommand() > 0;
            }
            return iscuccess;
        }

        /// <summary>
        /// 新增操作--只插入指定列
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <param name="columns">需要插入的列的数组</param>
        /// <returns></returns>
        public static bool InsertAnyClassColumns(List<T> dataList, string[] columns)
        {
            bool iscuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                iscuccess = client.Insertable<T>(dataList).InsertColumns(columns).ExecuteCommand() > 0;
            }
            return iscuccess;
        }

        /// <summary>
        /// 新增操作--空列不插入
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertAnyClassEmptyColumns(List<T> dataList)
        {
            bool iscuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                iscuccess = client.Insertable<T>(dataList).IgnoreColumns(ignoreNullColumn: true).ExecuteCommand() > 0;
            }
            return iscuccess;
        }

        #endregion

        #region 假删除

        public interface IDeleted
        {
            bool IsDeleted { get; set; }
        }

        public class Repository<Y> : SimpleClient<Y> where Y : class, IDeleted, new()
        {
            public Repository(ISqlSugarClient context = null) : base(context)
            {
                if (context == null)
                {
                    Context = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES);
                }
            }

            /// <summary>
            /// 假删除
            /// </summary>
            /// <returns></returns>
            public bool FalseDelete(Expression<Func<Y, bool>> expression)
            {
                return Context.Updateable<Y>()
                        .Where(expression)
                        .SetColumns(it => it.IsDeleted == true).ExecuteCommand() > 0;

            }
        }

        /// <summary>
        /// 根据所传类进行删除操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool DeleteClass(T dataList, Expression<Func<T, bool>> expression)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                isSuccess = client.Deleteable<T>().Where(expression).ExecuteCommand() > 0;
            }
            return isSuccess;
        }
        #endregion

        #region 查询

        /// <summary>
        /// 查询--无条件无分页
        /// </summary>
        /// <returns></returns>
        public static List<T> Query()
        {
            List<T> resList = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                resList = client.Queryable<T>().ToList();
            }
            return resList;
        }

        /// <summary>
        /// 查询--根据主键查询
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public static T QueryForKey(object key)
        {
            T resList = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                resList = client.Queryable<T>().InSingle(key);
            }
            return resList;
        }

        /// <summary>
        /// 查询--分页，不带条件
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public static List<T> QueryForPageList(int pageIndex, int pageSize, ref int totalCount)
        {
            List<T> resList = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                resList = client.Queryable<T>().ToPageList(pageIndex, pageSize, ref totalCount);
            }
            return resList;
        }

        /// <summary>
        /// 查询--分页,带条件
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        public static List<T> QueryForPageList(int pageIndex, int pageSize, ref int totalCount, Expression<Func<T, bool>> expression)
        {
            List<T> resList = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                resList = client.Queryable<T>().Where(expression).ToPageList(pageIndex, pageSize, ref totalCount);
            }
            return resList;
        }

        /// <summary>
        /// 查询--分页 根据SQL返回一条数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public static List<T> SelectForPageList(string sqlStr, int pageIndex, int pageSize, ref int totalCount)
        {
            List<T> resList = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                resList = client.Queryable<T>(sqlStr).ToPageList(pageIndex, pageSize, ref totalCount);
            }
            return resList;
        }

        /// <summary>
        /// 根据SQL返回一条数据
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <returns></returns>
        public static T QueryForSql(string sqlStr)
        {
            T res = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                res = client.SqlQueryable<T>(sqlStr).First();
            }
            return res;
        }

        /// <summary>
        /// 根据SQL返回集合
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <returns></returns>
        public static List<T> QueryForSqlToList(string sqlStr)
        {
            List<T> res = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LES))
            {
                res = client.SqlQueryable<T>(sqlStr).ToList();
            }
            return res;
        }

        /// <summary>
        /// 炼钢根据SQL返回集合
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <returns></returns>
        public static List<T> QueryForSqlToListLG(string sqlStr)
        {
            List<T> res = new();
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LG))
            {
                res = client.SqlQueryable<T>(sqlStr).ToList();
            }
            return res;
        }








        public static int QueryNums(Expression<Func<T, bool>> expression, SqlSugarClient client)
        {
            int total = 0;

            using (client)
            {
                total = client.Queryable<T>().Where(expression).Count();
            }

            return total;
        }
        #endregion

        #region 删除

        public static bool DeleteAny(Expression<Func<T, bool>> expression, SqlSugarClient client)
        {
            bool isSuccess;
            using (client)
            {
                isSuccess = client.Deleteable<T>().Where(expression).ExecuteCommand() > 0;
            }
            return isSuccess;
        }

        #endregion

        /// <summary>
        /// 用于拼接sql，dic的key:orderby(排序)，Static_Condition(静态条件),分页条件(pageSize,pageIndex),字段前加字符:e__等于,l__小于,g__大于,le__小于等于,ge__大于等于,L__模糊查询,LB__以..开头,LE__以..结尾,I__在..集合中,IN__不在..集合中,VN__字段为空,VY__字段不为空
        /// </summary>
        /// <param name="selectsql"></param>
        /// <param name="dic"></param>
        /// <param name="orderby"></param>
        /// <param name="isZupdate"></param>
        public static string SelectSql(string selectsql, Dictionary<string, object> dic, string orderby = null, bool isZupdate = false)
        {
            //更新指定字段的值的sql拼接
            string udColustrs = "";
            if (isZupdate)
            {
                StringBuilder udColus = new StringBuilder();
                //参数分隔符
                foreach (var k in dic)
                {
                    if (!k.Key.Contains("__"))
                    {
                        udColus.Append(k.Key);
                        udColus.Append("=");
                        udColus.Append(k.Key);
                        udColus.Append(",");
                    }
                }
                if (udColus.Length > 0)
                {
                    udColustrs = udColus.ToString();
                    udColustrs = udColustrs.Substring(0, udColustrs.Length - 1);
                }
            }
            StringBuilder partsql = new StringBuilder();
            partsql.Append(selectsql);//select..from..
            if (isZupdate) partsql.Append(" " + udColustrs + " ");//更新指定字段的值的sql拼接,set在selectsql中
            StringBuilder conditionsql = new StringBuilder();//where..
            GetCondition(dic, conditionsql);
            if (conditionsql.Length > 0)
            {
                partsql.Append(" WHERE ");
                partsql.Append(conditionsql);
            }
            if (!string.IsNullOrEmpty(orderby))//order..by..
            {
                partsql.Append(" ORDER BY ");
                partsql.Append(orderby);
            }
            return partsql.ToString();
        }
        /// <summary>
        /// 拼接条件，符合的 该方法会自动识别，不符合的可以自己拓展
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="partsql"></param>
        public static void GetCondition(Dictionary<string, object> dic, StringBuilder partsql)
        {
            //参数分隔符
            if (dic == null) return;
            bool havaCondition = false;
            List<string> removeColl = new List<string>();
            Dictionary<string, object> updateColl = new Dictionary<string, object>();
            foreach (var key in dic.Keys)
            {
                if (!key.Contains("__")) continue;
                if (!havaCondition && partsql.Length > 0) havaCondition = true;
                string flag = key.Substring(0, key.IndexOf("_"));//标志
                string field = key.Substring(key.IndexOf("_") + 2);//字段
                string param = key;
                if (key.Contains("."))
                    param = param.Replace('.', '_');
                switch (flag)
                {
                    case "e":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" = ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "ne":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" != ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "l":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" < ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "g":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" > ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "le":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" <= ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "ge":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" >= ");
                        partsql.Append("'" + dic[param] + "'");
                        break;
                    case "L":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" LIKE ");
                        partsql.Append("'" + dic[param] + "'");
                        updateColl[key] = "%" + dic[key].ToString() + "%";
                        break;
                    case "LB":
                        partsql.Append(field);
                        partsql.Append(" LIKE ");
                        partsql.Append("'" + dic[param] + "'");
                        updateColl[key] = dic[key].ToString() + "%";
                        break;
                    case "LE":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" LIKE ");
                        partsql.Append("'" + dic[param] + "'");
                        updateColl[key] = "%" + dic[key].ToString();
                        break;
                    case "I":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" IN ");
                        partsql.Append("('" + dic[param] + "')");
                        break;
                    case "IN":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" NOT IN ");
                        partsql.Append("('" + dic[param] + "')");
                        break;
                    case "VN":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" IS NULL");
                        removeColl.Add(key);
                        break;
                    case "VY":
                        if (havaCondition) partsql.Append(" and ");
                        partsql.Append(field);
                        partsql.Append(" IS NOT NULL");
                        removeColl.Add(key);
                        break;
                }
            }
            foreach (var str in removeColl)
            {
                dic.Remove(str);
            }
            foreach (var str in updateColl.Keys)
            {
                dic[str] = updateColl[str];
            }
            removeColl.Clear();
            updateColl.Clear();
            foreach (var key in dic.Keys)
            {
                if (key.Contains("."))
                {
                    updateColl.Add(key.Replace('.', '_'), dic[key]);
                    removeColl.Add(key);
                }
            }
            foreach (var str in removeColl)
            {
                dic.Remove(str);
            }
            foreach (var str in updateColl.Keys)
            {
                dic[str] = updateColl[str];
            }
            if (dic.ContainsKey("Static_Condition"))
            {
                if (partsql.Length > 0) partsql.Append(" and ");
                partsql.Append(dic["Static_Condition"]);
                dic.Remove("dic");
            }
        }



        #region 操作LES库
        /// <summary>
        /// 根据字典所传字段进行更新
        /// </summary>
        /// <param name="dic">字典键值对 key:数据表字段  value:要修改和修改标识值</param>
        /// <param name="tableName">表名字</param>
        /// <param name="key">主键或其他判断条件字段</param>
        /// <returns></returns>
        public static bool UpdToDictionaryLES(Dictionary<string, object> dic, string tableName, string key)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LG))
            {
                client.BeginTran();
                isSuccess = client.Updateable<T>(dic).AS(tableName).WhereColumns(key).ExecuteCommandHasChange();
                if (isSuccess)
                {
                    client.CommitTran();
                }
                else
                {
                    client.RollbackTran();
                }
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据所传类进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertClassLES(T dataList)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LG))
            {
                isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据所传集合进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertAnyClassLES(List<T> dataList)
        {
            bool isSuccess;
            using (SqlSugarClient client = DBConnectionConfig.SqlSugarClient(DBConnetionStrings.LG))
            {
                isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            }
            return isSuccess;
        }


        /// <summary>
        /// 事务--根据所传类进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertClassLESTranc(T dataList, SqlSugarClient client)
        {
            bool isSuccess;
            isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            return isSuccess;
        }

        /// <summary>
        /// 根据所传集合进行新增操作
        /// </summary>
        /// <param name="dataList">所传集合</param>
        /// <returns></returns>
        public static bool InsertAnyClassLESTranc(List<T> dataList, SqlSugarClient client)
        {
            bool isSuccess;
            isSuccess = client.Insertable<T>(dataList).ExecuteCommand() > 0;
            return isSuccess;
        }
        #endregion





    }
}
